const app = getApp()
var xrSdk = require('../../../vendor/hope-sdk/index.js');
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
const util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    webURL: config.service.imageUrl,
    // 订单实体
    order:null,  
    //上传地址
    serverUrl: config.service.upload,
    // 1.车身斜45°（含车牌及车身）照
    im1: '',
    // 2.轮胎鼓包特写图片
    im2: '',
    // 3.轮胎批次（DOT）图片
    im3: '',
    // 4.轮胎磨损情况
    im4: '',
    // 请输入轮胎问题
    description: '',
    // 订单ID
    order_id: 0,
    // 轮胎ID
    tyre_id: 0
  },
  result: function (e) {
    console.log(e)
    var that = this;
    var imgArr = e.detail.imgArr;
    if (imgArr != null && imgArr.length > 0) {
      let img = imgArr[0];
      if (img.indexOf("wxfile://tmp") == 0) {
        return;
      }
      let type = e.currentTarget.dataset.type;
      switch (type) {
        case "1":
          that.setData({
            im1: img
          });
          break;
        case "2":
          that.setData({
            im2: img
          });
          break;
        case "3":
          that.setData({
            im3: img
          });
          break;
        case "4":
          that.setData({
            im4: img
          });
          break;
      }
    }
  },
  remove: function (e) {
    var that = this;
    //移除图片
    console.log(e)
    //let index = e.detail.index;
    var type = e.currentTarget.dataset.type;
    switch (type) {
      case "1":
        that.setData({
          im1: ""
        });
        break;
      case "2":
        that.setData({
          im2: ""
        });
        break;
      case "3":
        that.setData({
          im3: ""
        });
        break;
      case "4":
        that.setData({
          im4: ""
        });
        break;
    }
  },
  descriptionInput:function(e){
    this.setData({
      description: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      order_id: options.order_id,
      tyre_id : options.tyre_id
    });
    that.init();
  },
  init:function(){
    var that = this;
    if(that.data.order_id > 0){
      xrRequest.request(app,{
        url: config.service.orderInfo,
        login: true,
        data: {
          id: that.data.order_id
        },
        success: function (res) {
          if (res.data.code == 1) {
            that.setData({
              description: res.data.data.description,
              im1: res.data.data.img1,
              im2: res.data.data.img2,
              im3: res.data.data.img3,
              im4: res.data.data.img4,
              order: res.data.data
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
            })
          }
        },
        fail: function (res) { },
        authfail: function (e) {
          wx.navigateTo({
            url: "../../auth/auth"
          })
        }
      })
    }
  },
  /**
   * 保存
   */
  saveClick:function(e){
    var that = this;
    if (util.isEmpty(that.data.tyre_id)) {
      wx.showToast({
        title: '轮胎不能为空',
        icon: 'none',
      })
      return;
    }
    xrRequest.request(app,{
      url: config.service.saveOrder,
      login: true,
      auth: true,
      data: {
        order_id: that.data.order_id,
        tyre_id: that.data.tyre_id,
        description: that.data.description,
        img1: that.data.im1,
        img2: that.data.im2,
        img3: that.data.im3,
        img4: that.data.im4,
      },
      success: function (res) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
      },
      fail: function (res) { 
        wx.showToast({
          title: res,
          icon: 'none',
        })
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  /**
   * 提交
   */
  submitClick:function(e){
    var that = this;
    if (util.isEmpty(that.data.tyre_id)) {
      wx.showToast({
        title: '轮胎不能为空',
        icon: 'none',
      })
      return;
    }
    if (util.isEmpty(that.data.im1)) {
      wx.showToast({
        title: '车身斜45°（含车牌及车身）照未上传',
        icon: 'none',
      })
      return;
    }
    if (util.isEmpty(that.data.im2)) {
      wx.showToast({
        title: '轮胎鼓包特写图片未上传',
        icon: 'none',
      })
      return;
    }
    if (util.isEmpty(that.data.im3)) {
      wx.showToast({
        title: '轮胎批次（DOT）图片未上传',
        icon: 'none',
      })
      return;
    }
    if (util.isEmpty(that.data.im4)) {
      wx.showToast({
        title: '轮胎磨损情况未上传',
        icon: 'none',
      })
      return;
    }
    if (util.isEmpty(that.data.description)) {
      wx.showToast({
        title: '请输入轮胎问题',
        icon: 'none',
      })
      return;
    }
    xrRequest.request(app,{
      url: config.service.applyOrder,
      login: true,
      auth: true,
      data: {
        order_id: that.data.order_id,
        tyre_id: that.data.tyre_id,
        description: that.data.description,
        img1: that.data.im1,
        img2: that.data.im2,
        img3: that.data.im3,
        img4: that.data.im4,
      },
      success: function (res) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          wx.redirectTo({
            url: '../success/success'
          });
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
       },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  read:function(e){
    wx.navigateTo({
      url: '../../article/info/info'
    })
  }
})